package com.doudou.oauth.exception;

import com.doudou.commons.entity.Result;
import com.doudou.oauth.util.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
 * @Author: 傻男人
 * @Date: 2020/5/20 14:32
 * @Version: 1.0
 * @Description: 自定义token相关的异常
 */
@Slf4j
@Component
public class SimpleOAuth2AuthenticationEntryPoint extends OAuth2AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException {
        log.info("无权限的异常信息为："+e.getMessage());
        e.printStackTrace();
        HttpUtils.writerError(new Result(HttpStatus.UNAUTHORIZED.value(), e.getMessage()),response);
    }
}
